<template>
    <div>
        <h1>用户投资添加</h1>
        <table class="table table-bordered" border="1">
            <tr>
                <td>项目名称</td>
                <td>{{I_Name}}</td>
            </tr>
            <tr>
                <td>投资总额度</td>
                <td>{{I_Budget}}</td>
            </tr>
            <tr>
                <td>已投资额度</td>
                <td>{{I_Receive}}</td>
            </tr>
            <tr>
                <td>剩余额度</td>
                <td>{{I_Residue}}</td>
            </tr>
            <tr>
                <td>投资金额</td>
                <td>
                    <input type="text" v-model="UI_Money" @change="Max()"/>
                </td>
            </tr>
            <tr>
                <td>预计最高收益</td>
                <td>{{I_max}}</td>
            </tr>
            <tr>
                <td>
                    <button class="btn btn-primary" @click="AddMoney()">投资</button>
                </td>
            </tr>
        </table>
    </div>
</template>
<script setup lang="ts">
import { useRoute } from 'vue-router';
import { ref,onMounted } from 'vue';
import axios from 'axios';
import {useUserStore} from '../../../store/index'

const userStore = useUserStore();


const UI_Money = ref(0);

const I_Name = ref('');
const I_Budget = ref(0);
const I_Receive = ref(0);
const I_Residue = ref(0);
const I_max = ref(0);


const route = useRoute();
const id = route.params.id;
console.log(id);

onMounted(()=>{
    Updshow();
    Max();
})


const AddMoney = ()=>{
    axios({
        method:'post',
        url:'http://localhost:57969/api/Invest/AddInvestUser',
        data:{

            I_Id:route.params.id,
            U_Id:userStore.id,
            UI_Money:UI_Money.value
        }

    })
    .then(res=>{
        if(res.data>0)
        {
            alert("投资成功");
        }
        else if(res.data==-1)
        {
            alert("找不到用户");
        }
        else
        {
            alert("投资失败");
        }
        console.log(res.data);
    })
    .catch(err=>{
        console.log(err);
    })  
    
}

const Updshow = ()=>{
    axios({
        method:'get',
        url:'http://localhost:57969/api/Invest/UpdShowInvestinfo',
        params:{
            id:route.params.id
        }
    })
    .then(res=>{
        console.log(res.data);
        I_Name.value = res.data.I_Name;
        I_Budget.value = res.data.I_Budget;
        I_Receive.value = res.data.I_Receive;
        I_Residue.value = res.data.I_Residue;
    })
    .catch(err=>{
        console.log(err);
    })
}

const Max = ()=>{
    I_max.value = UI_Money.value*0.6;
}
</script>
